package be.djdb.generators;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;


import be.djdb.Statics;
import be.djdb.generators.data.SuperItems;

/**
* @author Lieven Roegiers
* @copyright 2007
* @from JAVA_mylibs
*/
public class Menu extends SuperItems{
	private JMenu a_menu;
	private String actiefmenu;
	private Map<String,JMenuBar> dropdown = new LinkedHashMap<String,JMenuBar>();
	private Map<String,JMenu> menus = new LinkedHashMap<String, JMenu>();
	private Object jmenu;
	private JMenuBar dropdownmenu;
	 /**<%@ page contentType="text/html; charset=" %>
	 * @param String resourcebundlename <br>
	 * (name => name-lbl.properties)(name => name-tooltip.properties)<br>
	 * or with locale<br>
	 * (name => name-lbl_nl.properties)(name => name-tooltip_nl.properties)<br>
	 * or country with locale<br>
	 * (name => name-lbl_BE_nl.properties)(name => name-tooltip_BE_nl.properties)<br>
	 * 
	 * @see for dummy's
	 * DO NOT REMAKE THIS WITH YOU GRAPHICAL EDITOR YOU WIL BREAK MULTILANGUAGE
	 */
	public Menu(String resourcebunlename) {
		super(resourcebunlename);
	}
	/**
	 * @param contentpane  Jframe.getContentPane();
	 * @param desktopPane desktopPane = new JDesktopPane();
	 */
	private void menugenerator(Container contentpane,JDesktopPane desktopPane){
	       GroupLayout layout = new GroupLayout(contentpane);
	       contentpane.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
	        );
	}
	/**
	 * @param menu
	 * @param lbl
	 * @param item
	 * @return 
	 */
	public boolean addmenuitem(String menu,String lbl,AbstractButton item){
		if (item!=null){
			item.setText(getlblFor(lbl));
			return additem(menu, lbl,item);
		}else{
			return false;
		}
	}
	/**
	 * @param menu
	 * @param lbl
	 * @return
	 */
	public boolean addsubmenuitem(JMenu menu,String lbl){
		if (menu!=null){
			menu.setText(getlblFor(lbl));
			if (Statics.FOLLOW){followlog("createAmenu:=>"+lbl );}
			a_menu = getmenu(lbl);
			if(a_menu != null){
				actiefmenu = lbl;
				return false;
			}else {
				a_menu = menu;
				a_menu.setText(lbl);
				menus.put(lbl, a_menu);
				actiefmenu = lbl;
				return true;
			}
		}else{
			return false;
		}
	}
	public boolean addmenuitem(String menu,String lbl,AbstractButton item,ActionListener AnAction){
		item.setText(getlblFor(lbl));
		item.addActionListener(AnAction);
		return addmenuitem(menu, lbl,item);
	}
	private boolean additem(String menu,String lbl,AbstractButton item){
		if (Statics.FOLLOW){followlog( "additem Amenu To:=> "+ menu +" item:"+ lbl );	}
		if (menu.equals(this.actiefmenu)){
			if (Statics.FOLLOW){followlog( menu+" is exist" );	}
			item.setName(lbl);
			item.setToolTipText(getTooltipFor(lbl));
			a_menu.add(item);
			tools.add( item);
		}else{
			if (Statics.FOLLOW){followlog( "is not exist" );	}
			createAmenu(menu);
			item.setName(lbl);
			item.setToolTipText(getTooltipFor(lbl));
			a_menu.add(item);
			tools.add( item);
			if (Statics.FOLLOW){followlog("add menu:"+menu+"met label:"+lbl );}
			menus.put(menu,a_menu);
		}
		return false;
	}
	/**
	 * @param name the name of the menu that wil create
	 * @return wil return a "new"( if not exist) or the "exist"  menu 
	 */
	private JMenu createAmenu(String name){
     	if (Statics.FOLLOW){followlog("createAmenu:=>"+name );}
		a_menu = getmenu(name);
		if(a_menu != null){
			actiefmenu = name;
			return a_menu;
		}else {
			a_menu = new JMenu();
			a_menu.setText(getlblFor(name));
			menus.put(name, a_menu);
			actiefmenu = name;
			return a_menu;
		}
	}
	/**
	 * @param name returns a jmenu from the menu collection
	 * @return
	 */
	public JMenu getmenu(String name){
		return menus.get(name);
	}
	/**
	 * 
	 * @return create a jmenubar for all exist menus
	 */
	public JMenuBar getmenubar(){
			return getmenubar(menus.keySet());
	}
	/**
	 * 
	 * @param set
	 * @return create a jmenubar for all set that you want
	 */
	public JMenuBar getmenubar(Set<String> set){
			JMenuBar atoolBar = new JMenuBar();
			for (Iterator iter = set.iterator(); iter.hasNext();) {
				String x =(String)iter.next();
				atoolBar.add(getmenu(x));
				if (Statics.FOLLOW){followlog("createAmenu:=>"+x );}
			}
			atoolBar.add(a_menu);
			return atoolBar;
	}
	/**
	 * @param menu
	 */
	private void removeAllItems(String menu){
			createAmenu(menu).removeAll();
	}


}
